package com.zzptc.mzb.myapplication.Frament;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.zzptc.mzb.myapplication.Ada.UserAda;
import com.zzptc.mzb.myapplication.Bean.User;
import com.zzptc.mzb.myapplication.MainActivity;
import com.zzptc.mzb.myapplication.R;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;


public class BlankFragment1 extends Fragment {
    private ListView lv;
    private UserAda adapter;
    private  int permission=0;
   // String[] fruits={"橘子","西瓜","菠萝"};
    private List<User> userList=new ArrayList<>();
    //List<String> dataList=new ArrayList<>();
    public BlankFragment1() {

    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_blank_fragment1, container, false);
        initView(view);//新的变量接住碎片
        setList();
        return view;

    }
//提示事件你点击了
    private void setList() {
        SharedPreferences sp=getActivity().getSharedPreferences("userinfo",Context.MODE_PRIVATE);
        permission=sp.getInt("permission",-1);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                switch (permission){
                    case 1:
                        showToast("你点击了"+userList.get(i).getName());
                        break;
                    case 2:
                        if(userList.get(i).getPermission()==permission){
                            showToast("不可以对管理员进行操作");
                        }else {
                            showDialog(userList.get(i));
                        }
                        break;
                }
            }
        });
//        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//                showToast("你点击了"+userList.get(i).getName());
//            }
//        });
    }
    //对话框
    private void showDialog(final User user){
        AlertDialog.Builder builder=new AlertDialog.Builder(getContext())
                .setIcon(R.mipmap.timg2)
                .setTitle("删除用户")
                .setMessage("是否删该用户"+user.getName())
                .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    //删除数据库里的点击数据并刷新见面
                    public void onClick(DialogInterface dialogInterface, int i) {
                        LitePal.delete(User.class,user.getId());
                        userList.remove(user);
                        adapter.notifyDataSetChanged();
                        showToast("已删除该用户");
                    }
                })
                .setNegativeButton("取消",null);
builder.create().show();
    }
//输出结果 运用for循环  获取并显示数据
    private void initView(View view) {
       userList= LitePal.findAll(User.class);//
        //dataList.clear();
        //for (User u:userList){
          //  dataList.add(u.getId()+"账户"+u.getName()+"密码"+u.getPassword());
       // }
        lv=view.findViewById(R.id.lv);
        adapter=new UserAda(getContext(),R.layout.user,userList);
       // ArrayAdapter<String> adapter=new ArrayAdapter<String>(getContext(),android.R.layout.simple_dropdown_item_1line,dataList);
        lv.setAdapter(adapter);
    }
    private  void  showToast(String str){
        Toast.makeText(getContext(),str,Toast.LENGTH_SHORT).show();
    }
}
